var http = require('http');
var fs = require('fs');

// 1. 创建 Server
var server = http.createServer();

// 2. 监听 request 请求，设置请求处理函数
server.on('request', function(req, res){
    // Respone different content to different request
    var url = req.url;
    if (url === '/') {
        fs.readFile('./resource/index.html', function(err, data){
            if (err) {
                res.setHeader('Content-Type', 'text/plain; charset=utf-8');
                res.end('文件读取失败，请稍后重试！')
            } else {
                res.setHeader('Content-Type', 'text/html; charset=utf-8');
                res.end(data)
            }
        })
    } else {
        res.end('404 Not Found.')
    }

    
});

// 3. 绑定端口号，启动服务
server.listen(8080, function(){
    console.log('Server is ready, listening 8080');
});